<script setup>
import {onMounted, reactive, ref, defineProps, defineEmits, watch} from "vue";
import {ElMessage, ElMessageBox, ElLoading} from "element-plus";
import {FusunPicApi} from "@/api/fusun/fusunPic/fusunPic";
import {useUserStore} from "@/stores/userStores";
import {useRoute} from "vue-router";

const route = useRoute();
const visible = ref(false);
const dialogTitle = ref("");
//------------------------ 详情表单 ------------
const formRef = ref();
const form = ref({
  id: '', //主建uuid生成
  title: '', //图片名称
  remark: '', //备注
  fileSource: '', //source集合
  createTime: '', //${column.comment}
  updateTime: '', //${column.comment}
  createUser: '', //${column.comment}
  updateUser: '', //${column.comment}
});

const rules = {
  title: [{required: true, message: "图片名称不能为空", trigger: "blur"}],
  remark: [{required: true, message: "备注不能为空", trigger: "blur"}],
  fileSource: [{required: true, message: "source集合不能为空", trigger: "blur"}],
  updateTime: [{required: true, message: "不能为空", trigger: "blur"}],
  updateUser: [{required: true, message: "不能为空", trigger: "blur"}],
};
const emit = defineEmits(["loadData"]);

function submit(formRef) {
  formRef.validate(async (valid) => {
    if (valid) {
      // 判断是新增还是修改
      let res = null;
      if (form.value.id) {
        res = await FusunPicApi.edit(form.value);
      } else {
        res = await FusunPicApi.add(form.value);
      }

      if (!res.success) {
        ElMessage.error(res.message);
        return;
      }
      cancel();
      ElMessage.success(res.message);
      emit("loadData");
    } else {
      return false;
    }
  });
}

function cancel() {
  form.value = {};
  visible.value = false;
}

const getDetail = async (row) => {
  const res = await FusunPicApi.detail(row.id);
  form.value = res.data;
};
// -----------------暴露出去的方法-------------------
const openModal = (row, title) => {
  if (row) {
    form.value = row;
    getDetail(row);
  }
  dialogTitle.value = title;
  visible.value = true;
};

defineExpose({
  openModal,
});
</script>

<template>
  <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-model="visible"
      :title="dialogTitle"
      width="50%"
      :before-close="cancel"
  >
    <div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="title" label="图片名称">
          <el-input v-model="form.title" size="small"/>
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="form.remark" size="small"/>
        </el-form-item>
        <el-form-item prop="fileSource" label="source集合">
          <el-input v-model="form.fileSource" size="small"/>
        </el-form-item>
        <el-form-item prop="operationName" label="">
          <el-input disabled v-model="form.createTime" size="small"/>
        </el-form-item>
        <el-form-item prop="updateTime" label="">
          <el-input v-model="form.updateTime" size="small"/>
        </el-form-item>
        <el-form-item prop="updateUser" label="">
          <el-input v-model="form.updateUser" size="small"/>
        </el-form-item>

      </el-form>

      <div>
        <el-button type="primary" size="small" class="login-btn" plain @click="submit(formRef)">保存</el-button>
        <el-button type="success" size="small" class="login-btn" plain @click="cancel">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<style lang="scss"></style>



